package com.jams.goodox.activity.mine.image;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.view.View;

import com.jams.goodox.constant.Constants;
import com.jams.goodox.utils.ContentUtil;
import com.jams.goodox.utils.ToastUtil;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by Jin on 2016/6/14.
 * Description
 */
public class ImageUtil {

	public final static String DREAM = "dreamforlc";
	public final static String FOLDER = "cache";
	public final static String PARENT_PATH = Environment.getExternalStorageDirectory() + File.separator
			+  DREAM;
	public final static String ALBUM_PATH = PARENT_PATH + File.separator + FOLDER;

	/**
	 * 保存文件
	 *
	 * @param bm
	 * @param fileName
	 * @throws IOException
	 */
	public static void saveFile(Context context, Bitmap bm, String fileName) throws IOException {
		File dirFile = new File(ALBUM_PATH);
		if (!dirFile.exists()) {
			dirFile.mkdirs();
		}
		File myCaptureFile = new File(ALBUM_PATH + File.separator + fileName);
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
		bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);
		// 其次把文件插入到系统图库
		String mFileName = System.currentTimeMillis() + ".jpg";
		MediaStore.Images.Media.insertImage(context.getContentResolver(), bm, mFileName, null);
		// 最后通知图库更新
		context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + ALBUM_PATH + File.separator + mFileName)));
		bos.flush();
		bos.close();
	}


	public static void saveFile(Bitmap bm, String fileName, Context mContext, View view) throws IOException {
		File dirFile = new File(Constants.ALBUM_PATH);
		if (!dirFile.exists()) {
			dirFile.mkdir();
		}
		File myCaptureFile = new File(Constants.ALBUM_PATH + fileName);
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
		bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);
		// 其次把文件插入到系统图库
		MediaStore.Images.Media.insertImage(mContext.getContentResolver(), bm, fileName, null);
		// 最后通知图库更新
		mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + Constants.ALBUM_PATH
				+ fileName)));
		bos.flush();
		bos.close();
		view.setVisibility(View.GONE);
		ToastUtil.showShort(mContext,"保存成功");
	}

	public static String retrievePath(Context context, Intent sourceIntent, Intent dataIntent) {
		String picPath = null;
		try {
			Uri uri;
			if (dataIntent != null) {
				uri = dataIntent.getData();
				if (uri != null) {
					picPath = ContentUtil.getPath(context, uri);
				}
				if (isFileExists(picPath)) {
					return picPath;
				}

			}

			if (sourceIntent != null) {
				uri = sourceIntent.getParcelableExtra(MediaStore.EXTRA_OUTPUT);
				if (uri != null) {
					String scheme = uri.getScheme();
					if (scheme != null && scheme.startsWith("file")) {
						picPath = uri.getPath();
					}
				}
				if (!TextUtils.isEmpty(picPath)) {
					File file = new File(picPath);
					if (!file.exists() || !file.isFile()) {
					}
				}
			}
			return picPath;
		} finally {
		}
	}

	private static boolean isFileExists(String path) {
		if (TextUtils.isEmpty(path)) {
			return false;
		}
		File f = new File(path);
		if (!f.exists()) {
			return false;
		}
		return true;
	}

	/**
	 * 通过流得到bitmap
	 *
	 * @param uri
	 * @return
	 */
	public static Bitmap decodeUriAsBitmap(Context mContext, Uri uri) {
		Bitmap bitmap = null;
		try {
			bitmap = BitmapFactory.decodeStream(mContext.getContentResolver()
					.openInputStream(uri));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return null;
		}
		return bitmap;
	}

	public static Intent choosePicture() {
		Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
		intent.setType("image/*");
		return Intent.createChooser(intent, null);
	}

	/**
	 * @param bitmap 对象
	 * @param w 要缩放的宽度
	 * @param h 要缩放的高度
	 * @return newBmp 新 Bitmap对象
	 */
	public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h){
		int width = bitmap.getWidth();
		int height = bitmap.getHeight();
		Matrix matrix = new Matrix();
		float scaleWidth = ((float) w / width);
		float scaleHeight = ((float) h / height);
		matrix.postScale(scaleWidth, scaleHeight);
		Bitmap newBmp = Bitmap.createBitmap(bitmap, 0, 0, width, height,
				matrix, true);
		return newBmp;
	}
}
